<?php
/****************************************************************************

	COPYRIGHT(C) WWW.MAXSI.DK, MAXSI SOFTWARE, SORTIE 2009
	ALL RIGHTS RESERVED

	Maxsi Distribution ContentServer/ Branch
	Scripts running a Maxsi Distribution Compatible Content Server.	
	
	compress.php
	Uncompresses a block.

****************************************************************************/

	// Read parameters.

	$BlockId			=	md_RequestParam('blockid');
	$ContentArchive		=	md_RequestParam('contentarchive');
	
	if (!isset($BlockId))
	{
		die('Failure! No BlockId');
	}
	if (!isset($ContentArchive))
	{
		die('Failure! No ContentArchive');
	}

	// See if the block is locked. If it's not locked, then do NOT edit.
	
	$slot	=	md_sql('SELECT locked FROM '.md_GetDatabaseName('products').' WHERE contentarchive = "'.md_SlashGlobalVar($ContentArchive).'"');
	
	if ( $md_sql_total[$slot] )
	{
		if (!$md_sql_row[$slot]['locked'])
		{
			die('Failure! This content archive needs to be locked before it can be edited.');
		}		
	}
	else
	{
		// This content server is not supported. Fail!
		die('Failure! No such game!');
		
		// Todo: Possible hacking possibillity: People can download .php sources via this code.
	}
	
	$contentarchive		= stripslashes($ContentArchive);
	$file_name			= $contentarchive.'/'.$contentarchive.'_Block_'.$BlockId;
	
	if ( file_exists($file_name.'.raw') )
	{
		die('Success! File was already uncompressed.');
	}
	else
	{
		if ( !file_exists($file_name.'.MaxsiDistribution') )
		{
			die('Failure! Block did not exist!');
		}
		else
		{		
			$compressed		=	file_get_contents($file_name.'.MaxsiDistribution');
			if ( $compressed === false ) { die('Failure! Error reading '.$file_name.'.MaxsiDistribution'); }
			$uncompressed	=	gzuncompress($compressed);
			unset($compressed);
			if ( $uncompressed === false ) { die('Failure! Error uncompressing '.$file_name.'.MaxsiDistribution'); }
			$result			=	file_put_contents($file_name.'.raw',$uncompressed);
			unset($uncompressed);
			if ( $uncompressed === false ) { die('Failure! Error writing '.$file_name.'.raw'); }
			
			// Success!			
		}
		
	}
?>